#!/usr/bin/python
# -*- coding: utf-8 -*-


import sys
import pywapi
import urllib2
import feedparser


location_id = 'FRXX0147'
google_url = 'http://www.google.com/ig/api?weather='
astro_url = 'http://www.meteorologic.net/webmaster/xml/xml_file_29073.xml' #Données de seeign, voir http://www.meteorologic.net/meteo-astronomie/Chamb%E9ry_29073.html
posy = 170
address = '/home/gromain/DPF/'
font = address + 'Jellyka.ttf'
base = 'weather/base.jpg'
target = 'weather/weather.png'
picaddress = address + 'weather/pics/'
degree = unichr(176).encode("utf-8") + 'C'

print 'MAJ de la météo...'

yweather = pywapi.get_weather_from_yahoo(location_id, 'metric')
location_name = yweather['location']['city']
gweather = pywapi.get_weather_from_google(location_name, 'FR')
aweather = feedparser.parse(astro_url)

print 'Données météo récupérées...'

icon = picaddress + gweather['current_conditions']['icon'][19:]

updatefile = open(address + 'weather/update.sh' , 'w')
updatefile.write('#!/bin/bash \n\n')
updatefile.write('gm convert ')

updatefile.write('-font ' + font + ' -pointsize 125 -fill white -draw \"text 300,80\\\" '+ gweather['forecast_information']['city'].encode("utf-8") +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 100 -draw \"text 600,' + str(posy) +' \\\"Conditions\\\" \" ')
updatefile.write('-font ' + font + ' -draw \"image Over 750,' + str(posy - 50) +' 0,0 \\\"'+ icon +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 600,' + str(posy + 50) +'\\\" '+ gweather['current_conditions']['temp_c'].encode("utf-8") + degree +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 600,' + str(posy + 100) +'\\\" '+ gweather['current_conditions']['condition'].encode("utf-8") +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 600,' + str(posy + 150) +'\\\" '+ gweather['current_conditions']['wind_condition'].encode("utf-8") +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 600,' + str(posy + 200) +'\\\" '+ gweather['current_conditions']['humidity'].encode("utf-8") +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 600,' + str(posy + 250) +'\\\" Pression: '+ yweather['atmosphere']['pressure'].encode("utf-8") +'mb\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 600,' + str(posy + 300) +'\\\" Visibilité: '+ yweather['atmosphere']['visibility'].encode("utf-8") +'km\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 100 -draw \"text 600,' + str(posy + 380) +'\\\"Ephéméride \\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 90 -draw \"text 600,' + str(posy + 430) +'\\\" Lever: '+ yweather['astronomy']['sunrise'] +'\\\" \" ')
updatefile.write('-font ' + font + ' -pointsize 90 -draw \"text 600,' + str(posy + 480) +'\\\" Coucher: '+ yweather['astronomy']['sunset'] +'\\\" \" ')

for i in range(0,4):
	day = gweather['forecasts'][i]['day_of_week'][:-1].capitalize().encode("utf-8") + ':'
	condition = gweather['forecasts'][i]['condition'].encode("utf-8")
	low = gweather['forecasts'][i]['low'].encode("utf-8")
	high = gweather['forecasts'][i]['high'].encode("utf-8")
	icon = picaddress + gweather['forecasts'][i]['icon'][19:]
	seeing = aweather.entries[0]['seeing_soir']
	updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 40,' + str(posy) +'\\\"'+ day +'\\\" \" ')
	updatefile.write('-font ' + font + ' -draw \"image Over 190,' + str(posy + 15) +' 0,0 \\\"'+ icon +'\\\" \" ')
	updatefile.write('-font ' + font + ' -pointsize 60 -draw \"text 230,' + str(posy + 53) +'\\\" Seeing:'+ seeing +'\\\" \" ')	
	updatefile.write('-font ' + font + ' -pointsize 80 -draw \"text 130,' + str(posy) +'\\\"'+ condition +'\\\" \" ')
	posy = posy + 40
	updatefile.write('-font ' + font + ' -pointsize 75 -draw \"text 60,' + str(posy) +'\\\" Max: '+ high +'\\\" \" ')
	posy = posy + 40
	updatefile.write('-font ' + font + ' -pointsize 75 -draw \"text 60,' + str(posy) +'\\\" Min: '+ low +'\\\" \" ')
	posy = posy + 60

updatefile.write('-font ' + font + ' -pointsize 45 -draw \"text 870,770\\\"MAJ: '+ gweather['forecast_information']['forecast_date'].encode("utf-8") +'\\\" \" ')
updatefile.write(address + base + ' ')
updatefile.write(address + target)
updatefile.close()

print 'Weather, script python terminé...'
